"""
KidsBuddy 1.0 命令行入口点
支持通过 python -m kidsbuddy 启动应用
"""

import asyncio
import sys
import argparse
from loguru import logger

from .config.logging import setup_logging
from .main import KidsBuddySupervisionSystem, main as main_func


def parse_arguments():
    """解析命令行参数"""
    parser = argparse.ArgumentParser(
        description="KidsBuddy 1.0 监督执行流程系统",
        prog="kidsbuddy"
    )
    
    parser.add_argument(
        "--student-id", 
        type=str, 
        default="student_001",
        help="学生ID，用于标识会话"
    )
    
    parser.add_argument(
        "--camera", 
        type=int, 
        default=0,
        help="摄像头设备ID"
    )
    
    parser.add_argument(
        "--demo", 
        action="store_true", 
        help="运行演示模式"
    )
    
    return parser.parse_args()


async def run_application():
    """运行应用程序"""
    # 设置日志
    setup_logging()
    
    # 解析参数
    args = parse_arguments()
    
    try:
        if args.demo:
            # 运行演示模式
            logger.info("正在启动演示模式...")
            # 这里可以导入并运行demo.py中的函数
            from examples.demo import run_demo
            await run_demo()
        else:
            # 运行正常模式
            logger.info(f"正在启动监督系统，学生ID: {args.student_id}，摄像头ID: {args.camera}")
            await main_func(student_id=args.student_id, camera_source=args.camera)
    except Exception as e:
        logger.error(f"程序执行错误: {e}")
        sys.exit(1)


def main():
    """主函数"""
    try:
        asyncio.run(run_application())
    except KeyboardInterrupt:
        print("\n程序被用户中断")
    except Exception as e:
        print(f"程序错误: {e}")
    finally:
        print("程序已结束")


if __name__ == "__main__":
    main()